package com.example.websocket.another.mapper;

import com.example.websocket.another.entity.UserMessageEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMessageMapper {

    /**
     * 查询所有消息
     *
     * @return
     */
    @Select("SELECT * FROM user_message")
    @Results({@Result(property = "sendTo", column = "send_to"),
            @Result(property = "senderFrom", column = "sender_from"),
            @Result(property = "createTime", column = "create_time")})
    List<UserMessageEntity> getTopicMesage();


    @Select("SELECT * FROM user_message WHERE send_to = #{id}")
    @Results({@Result(property = "sendTo", column = "send_to"),
            @Result(property = "senderFrom", column = "sender_from"),
            @Result(property = "createTime", column = "create_time")})
    List<UserMessageEntity> getUserMessageBySendTo(Long id);

    @Insert("INSERT INTO user_message(send_to,message,sender_from,create_time,topic) VALUES(#{sendTo}, #{message}, #{senderFrom}, #{createTime}, #{topic})")
    void insertUserMessage(UserMessageEntity messageEntity);

    @Insert({
            "<script>",
            "INSERT INTO user_message(send_to,message,sender_from,create_time,topic) VALUES ",
            "<foreach collection='messageList' item='item' index='index' separator=','>",
            "(#{item.sendTo}, #{item.message}, #{item.senderFrom}, #{item.createTime}, #{item.topic})",
            "</foreach>",
            "</script>"
    })
    int insertCollectList(@Param(value = "messageList") List<UserMessageEntity> messageList);

//    @Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}")
//    void update(UserEntity user);

    @Delete("DELETE FROM user_message WHERE id =#{id}")
    void deleteMes(Long id);

}